|
|
|
| vntStr2 = "20" ' Value is a String
lblStrConcat.Caption = vntStr1 + vntStr2' Value is a String
End Sub | | 2. Note the following output when this code is run: 1020 |  | In this example, you have two variants, and each one is assigned a string that has a number in it. If you use the plus sign on the two variants, Visual Basic performs string concatenation just like you would expect. However, if one of the variants happens to be a string and the other happens to be numeric, youll get different results with the same operator. |
|
|
| String and Numeric with the Ampersand 1. Double-click the + Numbers command button to view the following code: | | Private Sub cmdVarNumber_Click ()
Dim vntStr1 As Variant
Dim vntStr2 As Variant
vntStr1 = "10"
vntStr2 = 20
lblVarNumber.Caption = vntStr1 + vntStr2
End Sub | | 2. Note the following output when this code is run: | | 30 |  | In this example, you get a different result30, an integer number. When you use a plus sign to concatenate two variant data types together, Visual Basic looks at each of the operands and determines whether either one is a numeric variable. If either one is a numeric variable, Visual Basic attempts to convert the other operand to a numeric variable. If its successful at this conversion, Visual Basic will perform integer arithmetic on the two operands. If its not successful at the conversion, youll receive a type mismatch error. |
|
|
|